package com.kymatrix.mpo.plus.bpm.server.config.mybatis;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kymatrix.mpo.plus.common.business.common.BasePageReq;
import com.kymatrix.mpo.plus.common.business.common.SortingField;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * MyBatis 工具类
 */
public class MyBatisUtils {

    public static <T> Page<T> buildPage(BasePageReq req) {
        return buildPage(req, null);
    }

    public static <T> Page<T> buildPage(BasePageReq basePageReq, Collection<SortingField> sortingFields) {
        // 页码 + 数量
        Page<T> page = new Page<>(basePageReq.getCurrent(), basePageReq.getPageSize());
        // 排序字段
        if (!CollectionUtil.isEmpty(sortingFields)) {
            page.addOrder(sortingFields.stream().map(sortingField -> SortingField.ORDER_ASC.equals(sortingField.getOrder()) ?
                            OrderItem.asc(sortingField.getField()) : OrderItem.desc(sortingField.getField()))
                    .collect(Collectors.toList()));
        }
        return page;
    }


}
